package com.ruoyi.project.setsystem.rolepower.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

import com.ruoyi.project.setsystem.rolepower.domain.RolePower;
import com.ruoyi.project.setsystem.rolepower.service.IRolePowerService;

import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.domain.UserRole;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 大帅比
 * @date 2020-09-20 11:52
 * @desc 百战程序员 http://www.itbaizhan.com
 */
@Controller
@RequestMapping("/setsystem/rolepower")
public class RolePowerController extends BaseController {
    private String prefix="setsystem/rolepower";
    @Autowired
    private IRolePowerService rolePowerService;
    @Autowired
    private IUserService userService;
    @RequiresPermissions("setsystem:rolepower:view")
    @GetMapping()
    public String rolepower(){
        return prefix+"/rolepower";
    }

    @RequiresPermissions("setsystem:rolepower:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo demo01(RolePower rolePower){
        startPage();
        List<RolePower> all = rolePowerService.All(rolePower);
        System.out.println(all);
        return getDataTable(all);
    }
    @Log(title = "角色管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("setsystem:rolepower:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RolePower rolePower)
    {
        List<RolePower> list = rolePowerService.All(rolePower);
        ExcelUtil<RolePower> util = new ExcelUtil<RolePower>(RolePower.class);
        return util.exportExcel(list, "角色数据");
    }

    /**
     * 新增角色
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存角色
     */
    @RequiresPermissions("setsystem:rolepower:add")
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated RolePower rolePower)
    {
        if (UserConstants.ROLE_NAME_NOT_UNIQUE.equals(rolePowerService.checkRoleNameUnique(rolePower)))
        {
            return error("新增角色'" + rolePower.getRoleName() + "'失败，角色名称已存在");
        }
        else if (UserConstants.ROLE_KEY_NOT_UNIQUE.equals(rolePowerService.checkRoleKeyUnique(rolePower)))
        {
            return error("新增角色'" + rolePower.getRoleName() + "'失败，角色权限已存在");
        }
        return toAjax(rolePowerService.insertRole(rolePower));

    }

    /**
     * 修改角色
     */
    @GetMapping("/edit/{roleId}")
    public String edit(@PathVariable("roleId") Long roleId, ModelMap mmap)
    {
        mmap.put("rolepower", rolePowerService.selectRoleById(roleId));
        return prefix + "/edit";
    }

    /**
     * 修改保存角色
     */
    @RequiresPermissions("setsystem:rolepower:edit")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated RolePower rolePower)
    {
        rolePowerService.checkRoleAllowed(rolePower);
        if (UserConstants.ROLE_NAME_NOT_UNIQUE.equals(rolePowerService.checkRoleNameUnique(rolePower)))
        {
            return error("修改角色'" + rolePower.getRoleName() + "'失败，角色名称已存在");
        }
        else if (UserConstants.ROLE_KEY_NOT_UNIQUE.equals(rolePowerService.checkRoleKeyUnique(rolePower)))
        {
            return error("修改角色'" + rolePower.getRoleName() + "'失败，角色权限已存在");
        }
        return toAjax(rolePowerService.updateRole(rolePower));
    }



    @RequiresPermissions("setsystem:rolepower:remove")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(rolePowerService.deleteRoleByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 校验角色名称
     */
    @PostMapping("/checkRoleNameUnique")
    @ResponseBody
    public String checkRoleNameUnique(RolePower rolePower)
    {
        return rolePowerService.checkRoleNameUnique(rolePower);
    }

    /**
     * 校验角色权限
     */
    @PostMapping("/checkRoleKeyUnique")
    @ResponseBody
    public String checkRoleKeyUnique(RolePower rolePower)
    {
        return rolePowerService.checkRoleKeyUnique(rolePower);
    }

    /**
     * 选择菜单树
     */
    @GetMapping("/selectMenuTree")
    public String selectMenuTree()
    {
        return prefix + "/tree";
    }

    /**
     * 角色状态修改
     */
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("setsystem:rolepower:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(RolePower rolePower)
    {
        rolePowerService.checkRoleAllowed(rolePower);
        return toAjax(rolePowerService.changeStatus(rolePower));
    }

    /**
     * 分配用户
     */
    @RequiresPermissions("setsystem:rolepower:edit")
    @GetMapping("/authUser/{roleId}")
    public String authUser(@PathVariable("roleId") Long roleId, ModelMap mmap)
    {
        mmap.put("rolepower", rolePowerService.selectRoleById(roleId));
        return prefix + "/authUser";
    }

    /**
     * 查询已分配用户角色列表
     */
    @RequiresPermissions("setsystem:rolepower:list")
    @PostMapping("/authUser/allocatedList")
    @ResponseBody
    public TableDataInfo allocatedList(User user)
    {
        startPage();
        List<User> list = userService.selectAllocatedList(user);
        System.out.println("ssderf");
        return getDataTable(list);
    }
//    public List<User> allocatedList(User user){
//        List<User> list = userService.selectAllocatedList(user);
//        return list;
//    }

    /**
     * 取消授权
     */
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PostMapping("/authUser/cancel")
    @ResponseBody
    public AjaxResult cancelAuthUser(UserRole userRole)
    {
        return toAjax(rolePowerService.deleteAuthUser(userRole));
    }

    /**
     * 批量取消授权
     */
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PostMapping("/authUser/cancelAll")
    @ResponseBody
    public AjaxResult cancelAuthUserAll(Long roleId, String userIds)
    {
        return toAjax(rolePowerService.deleteAuthUsers(roleId, userIds));
    }

    /**
     * 选择用户
     */
    @GetMapping("/authUser/selectUser/{roleId}")
    public String selectUser(@PathVariable("roleId") Long roleId, ModelMap mmap)
    {
        mmap.put("rolepower", rolePowerService.selectRoleById(roleId));
        return prefix + "/selectUser";
    }

    /**
     * 查询未分配用户角色列表
     */
    @RequiresPermissions("setsystem:rolepower:list")
    @PostMapping("/authUser/unallocatedList")
    @ResponseBody
    public TableDataInfo unallocatedList(User user)
    {
        startPage();
        List<User> list = userService.selectUnallocatedList(user);
        return getDataTable(list);
    }

    /**
     * 批量选择用户授权
     */
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PostMapping("/authUser/selectAll")
    @ResponseBody
    public AjaxResult selectAuthUserAll(Long roleId, String userIds)
    {
        return toAjax(rolePowerService.insertAuthUsers(roleId, userIds));
    }


}
